import { getStatus, Logout } from 'api/login'
import { GetUserPlayList, GetLikeList } from 'api/user'
import { DoLikeAsync } from 'api/song'
import { clearCookie } from 'common/js/utils'

const state = {
  name: null, // 名称
  avatar: null, // 头像
  backgroundImg: null, // 背景图片
  userId: null, //  用户ID
  // loading: false, //  登录中
  favoriteSongList: null, //  我喜欢的音乐
  favoriteSongIds: [], //  当前我喜欢的音乐的ids 用于判断当前音乐能否喜欢
  playList: [] //  用户歌单数据
}

const actions = {
  change_state ({ commit }, { key, value }) {
    if (key) commit('CHANGE_STATE', { key, value })
  },
  getStatus ({ commit, dispatch, rootState }) {
    return getStatus().then(res => {
      if (res.profile) {
        const { nickname, avatarUrl, userId, backgroundUrl } = res.profile
        const user = Object.assign({}, rootState.user, {
          name: nickname,
          avatar: avatarUrl,
          userId: userId,
          backgroundImg: backgroundUrl
        })
        commit('CHANGE_STATE', { key: 'user', value: user }, { root: true })
        Promise.resolve(true)
        dispatch('getUserPlayList')
        dispatch('getLikeList')
      }
    }).catch(err => {
      return Promise.reject(err)
    })
  },
  getUserPlayList ({ commit, state }) {
    GetUserPlayList(state.userId).then(list => {
      if (list.length && list[0].name.includes(state.name + '喜欢的音乐')) {
        commit('CHANGE_STATE', { key: 'favoriteSongList', value: list[0] })
        list.splice(0, 1)
      }
      commit('CHANGE_STATE', { key: 'playList', value: list })
    }).catch(err => {
      console.log(err)
    })
  },
  getLikeList ({ commit }) {
    GetLikeList().then(ids => {
      commit('CHANGE_STATE', { key: 'favoriteSongIds', value: ids })
    }).catch(err => {
      console.log(err)
    })
  },
  Logout ({ commit }) {
    Logout()

    commit('CHANGE_STATE', {
      key: 'user',
      value: {
        name: null,
        avatar: null,
        backgroundImg: null,
        userId: null,
        favoriteSongList: null,
        favoriteSongIds: [],
        playList: []
      }}, { root: true })

    /*eslint-disable*/
    document.cookie.match(/[^ =;]+(?=\=)/g).forEach(item => {
      clearCookie(item)
    })
    /*eslint-disable*/
  },
  addFavoriteSongIds ({ commit, state }, id) {
    DoLikeAsync(id, true).then(res => {
      let ids = state.favoriteSongIds.slice()
      ids.push(id)
      commit('SET_FAVORITE_SONG_IDS', ids)
    }).catch(err => {
      console.log(err)
    })
  },
  delFavoriteSongIds ({ commit, state }, id) {
    DoLikeAsync(id, false).then(res => {
      const Index = state.favoriteSongIds.findIndex(item => item === id)
      if (Index > -1) {
        const ids = state.favoriteSongIds.slice()
        ids.splice(Index, 1)
        commit('SET_FAVORITE_SONG_IDS', ids)
      }
    }).catch(err => {
      console.log(err)
    })
  }
}

const mutations = {
  CHANGE_STATE: (state, { key, value }) => {
    state[key] = value
  },
  SET_FAVORITE_SONG_IDS (state, ids) {
    state.favoriteSongIds = ids
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
